package com.bohua.child.service.impl;

import com.bohua.child.common.Lay;
import com.bohua.child.common.Msg;
import com.bohua.child.dao.LeaveMapper;
import com.bohua.child.entity.Leave;
import com.bohua.child.service.LeaveService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 请假表服务实现
 * @author shichenyang
 * @email gyv12345@163.com
 * @date 2019/01/17
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class LeaveServiceImpl implements LeaveService {
    @Resource
    private LeaveMapper leaveMapper;

    @Override
    public Msg addLeave(Leave leave) {
        int i=  leaveMapper.insert(leave);
        Msg msg=new Msg();
        if (i>0){
            msg.setMsg("添加成功");
        }else{
            msg.setCode(-1);
            msg.setMsg("添加失败");
        }
        return msg;
    }

    @Override
    public Msg updateLeave(Leave leave) {
        int i= leaveMapper.updateByParam(leave);
        Msg msg=new Msg();
        if (i>0){
            msg.setMsg("修改成功");
        }else{
            msg.setCode(-1);
            msg.setMsg("修改失败");
        }
        return msg;
    }

    @Override
    public Msg delLeave(Leave leave) {
        int i=leaveMapper.del(leave.getStudentId());
        Msg msg=new Msg();
        if (i>0){
            msg.setMsg("删除成功");
        }else{
            msg.setCode(-1);
            msg.setMsg("删除失败");
        }
        return msg;
    }

    @Override
    public Lay getLeaves(Leave leave, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<Leave> leaves=leaveMapper.list(leave);
        PageInfo info=new PageInfo(leaves);
        Lay lay=new Lay();
        lay.setPage(page);
        lay.setCount(info.getTotal());
        lay.setData(info.getList());
        return lay;
    }
}
